﻿#define UNINSTALL

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Peppers.ShellExtensions;
using System.Peppers.Win32;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Md5ShellExtension;

namespace ShellExtensionInstaller
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly[] assemblies = new Assembly[] 
                { 
                    Assembly.GetAssembly(typeof(ContextMenuBase)),
                    Assembly.GetAssembly(typeof(Md5ContextMenu)) 
                };
            foreach (Assembly assembly in assemblies)
            {
                try
                {
#if UNINSTALL
                    RegistrationServices regSvr = new RegistrationServices();
                    regSvr.UnregisterAssembly(assembly);
                    
                    GacInstaller installer = new GacInstaller();
                    if (!installer.RemoveAssemblyFromCache(Path.GetFileNameWithoutExtension(assembly.Location)))
                    {
                        Console.WriteLine("Did not find in GAC!");
                    }
                    
                    Console.WriteLine("Uninstall SUCCESS!");
#else
                    GacInstaller installer = new GacInstaller();
                    if (!installer.AddAssemblyToCache(assembly.Location))
                    {
                        throw new Exception("Could not install to GAC!");
                    }

                    RegistrationServices regSvr = new RegistrationServices();
                    regSvr.RegisterAssembly(assembly, AssemblyRegistrationFlags.None);

                    //Kill explorer
                    foreach (Process p in Process.GetProcessesByName("explorer"))
                    {
                        p.Kill();
                    }

                    Console.WriteLine("Install SUCCESS!");
#endif
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc);
                }
            }

            Console.ReadKey();
        }
    }
}